<?php

// Namespace

namespace Loop;


/**
 *
 */

class CBitlyApiProxy
{
	/**
	 *
	 */
	
	public function shortenUrl($longUrl)
	{
		// Prepare the URL for the API call
		
		$apiUrl = PREFERENCE_BITLY_API_URL;
		$apiUrl .= 'shorten/';
		$apiUrl .= '?login=' . PREFERENCE_BITLY_API_LOGIN. '&';
		$apiUrl .= 'apiKey=' . PREFERENCE_BITLY_API_KEY. '&';
		$apiUrl .= 'domain=' . PREFERENCE_BITLY_API_DOMAIN . '&';
		$apiUrl .= 'format=' . PREFERENCE_BITLY_API_FORMAT . '&';
		$apiUrl .= 'longUrl=' . urlencode($longUrl);
		
		
		// Call the API
		
		include_once(PREFERENCE_PATH_CORE . 'CHttpHelper.php');
		
		$httpHelper = new \Loop\CHttpHelper();
		$rawResult = $httpHelper->get($apiUrl);
		
		$result = json_decode($rawResult, true);
		
		
		// If it failed, return the long URL
		
		if ($result['status_code'] != '200')
		{
			return $longUrl;
		}
		
		
		return $result['data']['url'];
	}
}

?>
